package com.gsm.service.impl;

import com.gsm.bo.MenuTreeBo;
import com.gsm.dao.SysMenuDao;
import com.gsm.dao.SysMenuRoleDao;
import com.gsm.entity.Result;
import com.gsm.po.SysMenu;
import com.gsm.po.SysMenuRole;
import com.gsm.service.SysMenuService;
import com.gsm.utils.IdUtils;
import com.gsm.vo.MenuTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统菜单(SysMenu)表服务实现类
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;
    @Autowired
    private SysMenuRoleDao sysMenuRoleDao;
    @Autowired
    private IdUtils idUtils;

    @Override
    public List<SysMenu> selectMenuByUserId(Long userId) {
        List<SysMenu> list = sysMenuDao.selectMenuByUserId(userId);
        List<SysMenu> arrayList = new ArrayList<>();
        list.forEach(i -> {
            if (i.getMenuPid() == 0) {
                arrayList.add(i);
            }
        });
        for (SysMenu i : arrayList) {
            List<SysMenu> tmpList = new ArrayList<>();
            i.setSysMenuList(tmpList);
            list.forEach(j->{
                if(i.getMenuId() == j.getMenuPid()){
                    tmpList.add(j);
                }
            });
        }
        return arrayList;
    }

    @Override
    public Result selectSysMenuByRoleId(Long roleId) {
        List<MenuTreeBo> bos = sysMenuDao.selectMenuTreeByRoleId(roleId);
        List<MenuTreeVo> vos = new ArrayList<>();
        bos.forEach(i -> {
            if (i.getMenuPid() == 0) {
                MenuTreeVo vo = MenuTreeVo.builder()
                        .menuId(i.getMenuId())
                        .title(i.getMenuName())
                        .expand(true)
                        .checked(false)
                        .build();
                vos.add(vo);
            }
        });
        vos.forEach(i -> {
            List<MenuTreeVo> children = new ArrayList<>();
            i.setChildren(children);
            bos.forEach(j -> {
                if (i.getMenuId().longValue() == j.getMenuPid().longValue()) {
                    MenuTreeVo vo = MenuTreeVo.builder()
                            .menuId(j.getMenuId())
                            .title(j.getMenuName())
                            .expand(false)
                            .checked(j.getFlag())
                            .build();
                    children.add(vo);
                }
            });
        });

        return Result.success(vos);
    }

    @Override
    public Result updateSysMenuByRoleId(Long roleId, List<Long> menuIds) {
        sysMenuRoleDao.deleteSysMenuRoleByRoleId(roleId);
        if(menuIds!=null && menuIds.size()>0){
            List<SysMenuRole> list = new ArrayList<>();
            menuIds.forEach(i -> {
                SysMenuRole sysMenuRole = new SysMenuRole(idUtils.nextId(), roleId, i);
                list.add(sysMenuRole);
            });
            sysMenuRoleDao.insertSysMenuListByRoleId(list);
        }
        return Result.success();
    }

    @Override
    public Result insertSysMenu(SysMenu sysMenu) {
        long menuId = idUtils.nextId();
        sysMenu.setMenuId(menuId);
        sysMenuDao.insertSysMenu(sysMenu);
        return Result.success();
    }

    /**
     * 根据 主键 menuId 删除
     *
     * @param menuId 主键
     * @return
     */
    @Override
    public Result deleteSysMenuByMenuId(Long menuId) {
        sysMenuDao.deleteSysMenuByMenuId(menuId);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param sysMenu 实例对象
     * @return
     */
    @Override
    public Result updateSysMenuByMenuId(SysMenu sysMenu) {
        sysMenuDao.updateSysMenuByMenuId(sysMenu);
        return Result.success();
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param menuId 主键
     * @return
     */
    @Override
    public Result selectSysMenuByMenuId(Long menuId) {
        SysMenu sysMenu = sysMenuDao.selectSysMenuByMenuId(menuId);
        Result result = Result.success(sysMenu);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param sysMenu 实例对象
     * @return
     */
    @Override
    public Result selectSysMenuList(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuDao.selectSysMenuList(sysMenu);
        return Result.success(list);
    }
}